<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车 - 淘乐乐</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }

        body {
            background-color: #f5f5f5;
            color: #333;
        }

        /* 导航栏样式 */
        .main-nav {
            background: #ff3c3c;
            padding: 15px 0;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 15px;
        }

        .nav-content {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .logo {
            color: #fff;
            font-size: 24px;
            font-weight: bold;
            text-decoration: none;
        }

        /* 购物车主体样式 */
        .cart-header {
            background: white;
            padding: 15px 20px;
            border-radius: 8px;
            margin: 20px 0;
            display: grid;
            grid-template-columns: 50px 400px 150px 150px 150px 100px;
            align-items: center;
            font-weight: bold;
            color: #666;
        }

        .cart-item {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 15px;
            display: grid;
            grid-template-columns: 50px 400px 150px 150px 150px 100px;
            align-items: center;
            transition: all 0.3s ease;
        }

        .cart-item:hover {
            box-shadow: 0 2px 15px rgba(0,0,0,0.1);
        }

        /* 复选框样式 */
        .checkbox-wrapper {
            display: flex;
            align-items: center;
        }

        .checkbox-wrapper input[type="checkbox"] {
            width: 18px;
            height: 18px;
            cursor: pointer;
        }

        /* 商品信息样式 */
        .product-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .product-image {
            width: 100px;
            height: 100px;
            border-radius: 8px;
            overflow: hidden;
        }

        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }

        .product-details h3 {
            margin-bottom: 5px;
            font-size: 16px;
        }

        .product-specs {
            color: #999;
            font-size: 14px;
        }

        /* 价格样式 */
        .price {
            color: #ff3c3c;
            font-weight: bold;
        }

        /* 数量控制样式 */
        .quantity-controls {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .quantity-btn {
            width: 28px;
            height: 28px;
            border: 1px solid #ddd;
            background: white;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .quantity-btn:hover {
            background: #f5f5f5;
        }

        .quantity-input {
            width: 50px;
            height: 28px;
            text-align: center;
            border: 1px solid #ddd;
            border-radius: 4px;
        }

        /* 删除按钮样式 */
        .remove-btn {
            color: #999;
            cursor: pointer;
            transition: color 0.3s ease;
        }

        .remove-btn:hover {
            color: #ff3c3c;
        }

        /* 购物车底部样式 */
        .cart-footer {
            background: white;
            padding: 20px;
            border-radius: 8px;
            margin-top: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .cart-total {
            display: flex;
            align-items: center;
            gap: 30px;
        }

        .total-price {
            color: #ff3c3c;
            font-size: 24px;
            font-weight: bold;
        }

        .checkout-btn {
            background: #ff3c3c;
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s ease;
        }

        .checkout-btn:hover {
            background: #ff6666;
            transform: translateY(-2px);
        }

        /* 空购物车样式 */
        .empty-cart {
            text-align: center;
            padding: 50px 0;
            background: white;
            border-radius: 8px;
            margin: 20px 0;
        }

        .empty-cart i {
            font-size: 48px;
            color: #999;
            margin-bottom: 20px;
        }

        .empty-cart p {
            color: #666;
            margin-bottom: 20px;
        }

        .continue-shopping {
            display: inline-block;
            background: #ff3c3c;
            color: white;
            text-decoration: none;
            padding: 10px 25px;
            border-radius: 20px;
            transition: all 0.3s ease;
        }

        .continue-shopping:hover {
            background: #ff6666;
            transform: translateY(-2px);
        }

        /* 小计样式 */
        .subtotal {
            color: #ff3c3c;
            font-weight: bold;
        }

        /* 全选样式 */
        .select-all {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .select-all label {
            cursor: pointer;
        }

        .user-links {
            display: flex;
            align-items: center;
            gap: 20px;
        }

        .user-links a {
            color: white;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 5px;
            transition: opacity 0.3s ease;
        }

        .user-links a:hover {
            opacity: 0.8;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="main-nav">
        <div class="container">
            <div class="nav-content">
                <a href="淘乐乐商城.html" class="logo">淘乐乐</a>
                <div class="user-links">
                    <a href="个人中心.html"><i class="fas fa-user"></i> 个人中心</a>
                    <span style="color: white;">购物车</span>
                </div>
            </div>
        </div>
    </nav>

    <!-- 购物车内容区域 -->
    <div class="container">
        <!-- 购物车头部 -->
        <div class="cart-header">
            <div class="checkbox-wrapper">
                <input type="checkbox" id="selectAll">
                <label for="selectAll">全选</label>
            </div>
            <div>商品信息</div>
            <div>单价</div>
            <div>数量</div>
            <div>小计</div>
            <div>操作</div>
        </div>

        <!-- 购物车列表 -->
        <div class="cart-list">
            <!-- 这里将通过JavaScript动态添加商品 -->
        </div>

        <!-- 购物车底部 -->
        <div class="cart-footer">
            <div class="select-all">
                <input type="checkbox" id="selectAllBottom">
                <label for="selectAllBottom">全选</label>
            </div>
            <div class="cart-total">
                <div>
                    已选择 <span class="selected-count">0</span> 件商品
                </div>
                <div>
                    合计：<span class="total-price">¥0</span>
                </div>
                <button class="checkout-btn" onclick="location.href='订单界面.html'">结算</button>
            </div>
        </div>

        <!-- 空购物车状态 -->
        <div class="empty-cart" style="display: none;">
            <i class="fas fa-shopping-cart"></i>
            <p>购物车还是空的，去挑选喜欢的商品吧~</p>
            <a href="淘乐乐商城.html" class="continue-shopping">继续购物</a>
        </div>
    </div>

    <!-- JavaScript代码 -->
    <script>
        // 获取或初始化购物车数据
        let cartItems = JSON.parse(localStorage.getItem('cartItems')) || [];

        // 渲染购物车商品
        function renderCartItems() {
            const cartList = document.querySelector('.cart-list');
            cartList.innerHTML = '';

            cartItems.forEach((item, index) => {
                const cartItemHTML = `
                    <div class="cart-item">
                        <div class="checkbox-wrapper">
                            <input type="checkbox" class="item-checkbox">
                        </div>
                        <div class="product-info">
                            <div class="product-image">
                                <img src="${item.image}" alt="${item.name}">
                            </div>
                            <div class="product-details">
                                <h3>${item.name}</h3>
                                <p class="product-specs">${item.specs}</p>
                            </div>
                        </div>
                        <div class="price">¥${item.price}</div>
                        <div class="quantity-controls">
                            <button class="quantity-btn" onclick="updateQuantity(${index}, -1)">-</button>
                            <input type="number" class="quantity-input" value="${item.quantity}" min="1" onchange="updateQuantity(${index}, 0)">
                            <button class="quantity-btn" onclick="updateQuantity(${index}, 1)">+</button>
                        </div>
                        <div class="subtotal">¥${item.price * item.quantity}</div>
                        <div class="remove-btn" onclick="removeItem(${index})">
                            <i class="fas fa-trash"></i>
                        </div>
                    </div>
                `;
                cartList.insertAdjacentHTML('beforeend', cartItemHTML);
            });

            updateTotal();
            checkEmptyCart();
        }

        // 添加商品到购物车
        function addToCart(product) {
            cartItems.push(product);
            localStorage.setItem('cartItems', JSON.stringify(cartItems));
            renderCartItems();
        }

        // 更新商品数量
        function updateQuantity(index, change) {
            const input = document.querySelectorAll('.quantity-input')[index];
            let newValue = parseInt(input.value) + change;
            
            // 确保数量不小于1
            if (newValue < 1) newValue = 1;
            
            // 更新商品数量
            cartItems[index].quantity = newValue;
            localStorage.setItem('cartItems', JSON.stringify(cartItems));
            renderCartItems();
            
            // 更新小计和总计
            updateTotal();
        }

        // 更新总计
        function updateTotal() {
            let total = 0;
            let count = 0;
            
            document.querySelectorAll('.cart-item').forEach((item, index) => {
                if (item.querySelector('.item-checkbox').checked) {
                    const price = cartItems[index].price;
                    const quantity = cartItems[index].quantity;
                    total += price * quantity;
                    count += quantity;
                }
            });
            
            document.querySelector('.total-price').textContent = `¥${total}`;
            document.querySelector('.selected-count').textContent = count;
        }

        // 添加全选功能
        document.querySelectorAll('#selectAll, #selectAllBottom').forEach(checkbox => {
            checkbox.addEventListener('change', (e) => {
                const isChecked = e.target.checked;
                document.querySelectorAll('.item-checkbox').forEach(item => {
                    item.checked = isChecked;
                });
                updateTotal();
            });
        });

        // 单个商品选择状态改变时更新总计
        document.addEventListener('change', (e) => {
            if (e.target.classList.contains('item-checkbox')) {
                updateTotal();
            }
        });

        // 删除商品
        function removeItem(index) {
            if (confirm('确定要删除这个商品吗？')) {
                cartItems.splice(index, 1);
                localStorage.setItem('cartItems', JSON.stringify(cartItems));
                renderCartItems();
            }
        }

        // 页面加载时渲染购物车
        renderCartItems();
    </script>
</body>
</html> 